﻿using System;
using System.Data;
using System.Data.SqlClient;
using Pantry.Commons;
using Pantry.DataAccess;
using Pantry.Entity;

namespace Pantry.Business
{
    public sealed class TrustRegionBus : IDisposable
    {
        private TrustRegionDao _trustRegionDao;

        /// <summary>
        /// Initializes a new instance of the <see cref="TrustRegionBus"/> class.
        /// </summary>
        public TrustRegionBus()
        {
            _trustRegionDao = new TrustRegionDao();
        }
        public TrustRegionBus(string connectionString)
        {
            _trustRegionDao = new TrustRegionDao(connectionString);
        }
        public DataSet SelectAll()
        {
            _trustRegionDao = new TrustRegionDao();
            return _trustRegionDao.SelectAll();
        }
        /// <summary>
        /// Retrieves the trust region.
        /// </summary>
        /// <param name="trustRegionId">The trust region id.</param>
        /// <returns></returns>
        public TrustRegion RetrieveTrustRegion(int trustRegionId)
        {
            if (trustRegionId < 1)
            {
                return null;
            }

            var result = _trustRegionDao.RetrieveTrustRegion(trustRegionId);
            return result;
        }
        /// <summary>
        /// Creates the trust region.
        /// </summary>
        /// <param name="trustRegion">The trust region.</param>
        /// <returns></returns>
        public bool CreateTrustRegion(TrustRegion trustRegion)
        {
            try
            {
                int result = 0;
                if (trustRegion != null)
                {
                    result = _trustRegionDao.CreateTrustRegion(trustRegion);
                }
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Updates the trust region.
        /// </summary>
        /// <param name="trustRegion">The trust region.</param>
        /// <returns></returns>
        public bool UpdateTrustRegion(TrustRegion trustRegion)
        {
            try
            {
                var result = _trustRegionDao.UpdateTrustRegion(trustRegion);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Changes the trust region status.
        /// </summary>
        /// <param name="trustRegionId">The trust region id.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <returns></returns>
        public bool ChangeTrustRegionStatus(int trustRegionId, bool isActive)
        {
            var result = _trustRegionDao.ChangeTrustRegionStatus(trustRegionId, isActive);
            return (result != 0);
        }
        /// <summary>
        /// Checks the trust region is exists.
        /// </summary>
        /// <param name="trustRegionName">Name of the trust region.</param>
        /// <returns></returns>
        public bool CheckTrustRegionIsExists(string trustRegionName)
        {
            var result = _trustRegionDao.CheckTrustRegionIsExists(trustRegionName);
            return (result != 0);
        }
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                //_trustRegionDao.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
